var press_card = 1;
var press_qrcode = 1;
var kfid = "";
$(document).ready(function(){
    kfid = getUrlParam("kfid");
    var weixin = isWeiXin();
    if (weixin) {
        kefuQrcodeDetial();
    } else {
        errorNotice("访问环境异常","请使用微信访问当前链接!");
    }

    $("#qrcode").on({  
        touchstart: function(e) {
            // 长按事件触发  
            timeOutEvent = setTimeout(function() {  
                timeOutEvent = 0;
                kefuQrcodePress();
            }, 1000);  
        },  
        touchmove: function() {  
            clearTimeout(timeOutEvent);  
            timeOutEvent = 0;  
        },  
        touchend: function() {  
            clearTimeout(timeOutEvent);  
            if (timeOutEvent != 0) {  
            }  
            return false;  
        }  
    })
    $("#full_qrcode").on({  
        touchstart: function(e) {
            // 长按事件触发  
            timeOutEvent = setTimeout(function() {  
                timeOutEvent = 0;
                kefuQrcodePress();
            }, 1000);  
        },  
        touchmove: function() {  
            clearTimeout(timeOutEvent);  
            timeOutEvent = 0;  
        },  
        touchend: function() {  
            clearTimeout(timeOutEvent);  
            if (timeOutEvent != 0) {  
            }  
            return false;  
        }  
    })
});

// 监听当前页面的显示状态
document.addEventListener("visibilitychange", function () {
    var visibilityState = document.visibilityState=='hidden' ?0:1
    if (visibilityState === 0) {
        if (press_qrcode == 2 && press_card == 1) {
            press_card = 2;
        }
    } else if (visibilityState === 1) {
        if (press_card == 2) {
            kefuQrcodePressCard();
        }
    }
}, false);

function kefuQrcodeDetial() {
    var query = window.location.search;
    var qrcodeInfo = JSON.parse(localStorage.getItem("qrcode_"+kfid) || '{"kfid":""}');
    if (qrcodeInfo.kfid == "") {
        $.ajax({
            url:"https://bab.moreqifu.cn/api/version/1/kefu/work_qrcode"+query,
            data:{},
            success:function(result){
                if (result.status == "success") {
                    localStorage.setItem("qrcode_"+kfid,JSON.stringify(result.data));
                    renderQrcode(result.data);
                } else {
                    errorNotice("链接加载失败",result.msg);
                }
            }
        });
    } else {
        renderQrcode(qrcodeInfo);
    }
}

function renderQrcode(qrcodeInfo) {
    $("#article_error").hide();
    $(".content").show();
    $("title").html(qrcodeInfo.qrcode_title);
    if (qrcodeInfo.qrcode_style == 1) {
        $(".content-article").show();
        $(".content-article-full").hide();
        $("#content-article-header-img").attr("src",qrcodeInfo.qrcode_head_img);
        $(".content-article-header h3").html(qrcodeInfo.qrcode_nike);
        $(".content-article-header h5").html(qrcodeInfo.qrcode_describe);
        $(".content-article-qrcode").attr("src",qrcodeInfo.img_url);
        var weixin = isWeiXin();
        if (weixin) {
            $(".content-article-footer").html(qrcodeInfo.qrcode_notice);
        } else {
            var mobile = isMobile();
            if (mobile) {
                $(".content-article-footer").html("请截图到微信内扫码");
            } else {
                $(".content-article-footer").html("请使用微信扫描上方二维码");
            }
        }
    } else {
        $(".content-article").hide();
        $(".content-article-full").show();
        $("#full_qrcode").attr("src",qrcodeInfo.img_url);
        var weixin = isWeiXin();
        if (!weixin) {
            var mobile = isMobile();
            if (mobile) {
                $(".content-article-full").append('<div class="content-article-footer">请截图到微信内扫码</div>');
            } else {
                $(".content-article-full").append('<div class="content-article-footer">请使用微信扫描上方二维码</div>');
            }
        }
    }
}

function getUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");

            if (arr != null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    }
    else {
        return "";
    }
}

function kefuQrcodePress() {
    var qrcode_press = localStorage.getItem("work_qrcode_press_"+kfid) || 1;
    press_qrcode = 2;
    if (qrcode_press == 1) {
        var query = window.location.search;
        $.ajax({
            url:"https://bab.moreqifu.cn/api/version/1/kefu/work_qrcode_press"+query,
            data:{},
            success:function(result){
                if (result.status == "success") {
                    localStorage.setItem("work_qrcode_press_"+kfid,2);
                } else {
                }
            }
        });
    }
}

function kefuQrcodePressCard() {
    var work_qrcode_press_card = localStorage.getItem("work_qrcode_press_card"+kfid) || 1;
    if (work_qrcode_press_card == 1) {
        var query = window.location.search;
        $.ajax({
            url:"https://bab.moreqifu.cn/api/version/1/kefu/work_qrcode_press_card"+query,
            data:{},
            success:function(result){
                if (result.status == "success") {
                    localStorage.setItem("work_qrcode_press_card"+kfid,2);
                }
            }
        });
    }
}

function errorNotice(title,message) {
    $(".content").hide();
    $("#article_error").show();
    $("#article_error").html('<div class="page"><div class="weui-msg"><div class="weui-msg__icon-area"><i class="weui-icon-warn weui-icon_msg-primary"></i></div><div class="weui-msg__text-area"><h2 class="weui-msg__title">'+title+'</h2><p class="weui-msg__desc">'+message+'</p></div></div></div>');
}

//判断是否是微信浏览器的函数
function isWeiXin(){
    var ua = navigator.userAgent.toLowerCase()
    var isWXWork = ua.match(/wxwork/i) == 'wxwork'
    var isWeixin = !isWXWork && ua.match(/micromessenger/i) == 'micromessenger'
    return isWeixin;
}

function isMobile() {
    var mobile_flag = true;
    if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|XiaoMi|IEMobile)/i)) {
        mobile_flag = true;
    } else {
        mobile_flag = false;
    }
    var screen_width = window.screen.availWidth;
    // var screen_height = window.screen.height;

    //根据屏幕分辨率判断是否是手机
    if(screen_width < 700){
        mobile_flag = true;
    }
    return mobile_flag;
}